<?php

/**
 * job actions.
 *
 * @package    maximous
 * @subpackage job
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class job_requestActions extends MaxFrontendActions
{

    public function preExecute()
    {
        parent::preExecute();
        if ($this->maxUser)
        {
            $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::JobRequest);
        }
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        //check access
        $this->formFilter = new JobRequestFrontendFormFilter();
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query = $this->formFilter->buildQuery($this->formFilter->getValues());
        $query->orderBy('id desc');
        $page  = $request->getParameter('page', 1);
        $this->pager = $this->createPager('JobRequest', $query, $page, sfConfig::get('app_record_per_page'));
    }

    public function executeAddJobRequest(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canAdd(), $this->i18n->__("you dont't have permission to access this page"));
        $this->jobRequestForm = new JobRequestForm(null, array('maxUser' => $this->maxUser));
        if ($request->isMethod(sfWebRequest::POST))
        {
            $jobFormData = $request->getParameter($this->jobRequestForm->getName());
            $this->jobRequestForm->bind($jobFormData);
            if ($this->jobRequestForm->isValid())
            {
                $this->jobRequestForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('add job reqeust successful'));
            }
        }
    }

    public function executeEditJobRequest(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canEdit(), $this->i18n->__("you dont't have permission to access this page"));
        $jobRequest = $this->checkJobRequest($request);
        $this->jobRequestForm = new JobRequestForm($jobRequest, array('maxUser' => $this->maxUser));
        if ($request->isMethod(sfWebRequest::POST))
        {
            $jobRequestFormData = $request->getParameter($this->jobRequestForm->getName());
            $this->jobRequestForm->bind($jobRequestFormData);
            if ($this->jobRequestForm->isValid())
            {
                $this->jobRequestForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('edit job request successful'));
            }
        }
    }

    public function executeDeleteJobRequest(sfWebRequest $request)
    {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canDelete(), $this->i18n->__("you dont't have permission to access this page"));
        $jobRequest = $this->checkJobRequest($request);
        // delete job object
        $jobRequest->delete();
        $this->getUser()->setFlash('message_success', $this->i18n->__('edit job Request successful'));
        $this->redirect($request->getReferer());
    }

    public function executeShowMyJobRequests(sfWebRequest $request)
    {
        $userId = $this->maxUser->getUserDataByIndex('id');
        $page   = $request->getParameter('page');
        $query  = JobRequestTable::getInstance()->getUserJobRequests($userId);
        $this->pager = $this->createPager('Job', $query, $page, sfConfig::get('app_home_jobs_per_page'));
    }

    public function executeShowJobRequest(sfWebRequest $request)
    {
        $watchedJobRequest = $this->getUser()->getAttribute('watched_job_request', array());
        $jobTitle   = $request->getParameter('jtitle');
        $jobRequest = JobRequestTable::getInstance()->findOneByTitle($jobTitle);
        $this->forward404Unless($jobRequest);
        if (!in_array($jobRequest->getId(), $watchedJobRequest))
        {
            $jobRequest->increamentVisits();
            $jobRequest->save();
            $watchedJobRequest[] = $jobRequest->getId();
            $this->getUser()->setAttribute('watched_job_request', $watchedJobRequest);
        }
        $this->jobRequest = $jobRequest;
    }

    /**
     *  check user if can access this job if not the function do forward404 else it return the job object
     * @param sfWebRequest $request
     * @return JobRequest 
     */
    private function checkJobRequest(sfWebRequest $request)
    {
        $jobTitle   = $request->getParameter('jtitle');
        $userId     = $this->maxUser->getUserDataByIndex('id');
        $jobRequest = JobRequestTable::getInstance()->findOneByTitleAndUserId($jobTitle, $userId);
        $this->forward404Unless($jobRequest);
        return $jobRequest;
    }

}
